//
//  SmsCloudSearchController.m
//  smsCloud
//
//  Created by 蓝鳍互娱 on 2024/7/19.
//

#import "SmsCloudSearchController.h"
#import "SmsCloudPhoneController.h"
#import "SmsCloudMainNavView.h"
#import "SmsCloudSearchSearchView.h"
#import "SmsCloudTableView.h"
#import "SmsCloudSearchResultView.h"
@interface SmsCloudSearchController ()
@property (nonatomic,strong) SmsCloudMainNavView *smsCloudSearchNavView;
@property (nonatomic,strong) SmsCloudSearchSearchView *smsCloudSearchSearchView;
@property (nonatomic,strong) SmsCloudTableView *smsCloudTableView;
@property (nonatomic,strong) SmsCloudSearchResultView *smsCloudSearchResultView;

@property (nonatomic,copy) NSString *search;
@end

@implementation SmsCloudSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#F0F3F6"];
    [self smsCloudInitView];
}
- (SmsCloudMainNavView *)smsCloudSearchNavView{
    if(!_smsCloudSearchNavView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchNavView = [[SmsCloudMainNavView alloc] init];
        _smsCloudSearchNavView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
        _smsCloudSearchNavView.smsCloudNavTitle = @"搜索结果";
        _smsCloudSearchNavView.SmsCloudMainBackBlock = ^{
            [weakSelf.navigationController popViewControllerAnimated:true];
        };
    }
    return _smsCloudSearchNavView;
}
- (SmsCloudSearchSearchView *)smsCloudSearchSearchView{
    if(!_smsCloudSearchSearchView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchSearchView = [[SmsCloudSearchSearchView alloc] init];
        _smsCloudSearchSearchView.SmsCloudSearchBlock = ^(NSString * _Nonnull smsCloudSearch) {
            weakSelf.search = smsCloudSearch;
            [weakSelf smsCloudResult];
        };
    }
    return _smsCloudSearchSearchView;
}
-(SmsCloudTableView *)smsCloudTableView{
    if (!_smsCloudTableView) {
        __weak __typeof__(self) weakSelf = self;
        _smsCloudTableView = [[SmsCloudTableView alloc] init];
        _smsCloudTableView.backgroundColor = UIColor.whiteColor;
        _smsCloudTableView.SmsCloudProductBlock = ^(SmsCloudProductModel * _Nonnull smsCloudProductMdl) {
            SmsCloudPhoneController *smsCloudPhoneCtrl = [[SmsCloudPhoneController alloc] init];
            smsCloudPhoneCtrl.smsCloudProductMdl = smsCloudProductMdl;
            [weakSelf.navigationController pushViewController:smsCloudPhoneCtrl animated:true];
        };
    }
    return _smsCloudTableView;
}
-(SmsCloudSearchResultView *)smsCloudSearchResultView{
    if (!_smsCloudSearchResultView) {
        __weak __typeof__(self) weakSelf = self;
        _smsCloudSearchResultView = [[SmsCloudSearchResultView alloc] init];
        _smsCloudSearchResultView.backgroundColor = UIColor.whiteColor;
        _smsCloudSearchResultView.hidden = true;
        _smsCloudSearchResultView.SmsCloudProductBlock = ^(SmsCloudProductModel * _Nonnull smsCloudProductMdl) {
            SmsCloudPhoneController *smsCloudPhoneCtrl = [[SmsCloudPhoneController alloc] init];
            smsCloudPhoneCtrl.smsCloudProductMdl = smsCloudProductMdl;
            [weakSelf.navigationController pushViewController:smsCloudPhoneCtrl animated:true];
        };
    }
    return _smsCloudSearchResultView;
}
- (void)smsCloudInitView{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(smsCloudEndEdit)];
    [self.view addGestureRecognizer:tap];
    
    [self.view addSubview:self.smsCloudSearchNavView];
    [self.view addSubview:self.smsCloudSearchSearchView];
    [self.view addSubview:self.smsCloudTableView];
    [self.view addSubview:self.smsCloudSearchResultView];

    [self.smsCloudSearchNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:88] + [SmsCloudHead SmsCloudTopSafeH]));
    }];
    [self.smsCloudSearchSearchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudSearchNavView.mas_bottom);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:125]));
    }];
    UIView * smsCloudSearchSearcHeadView = [[UIView alloc] init];
    smsCloudSearchSearcHeadView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
    [self.view addSubview:smsCloudSearchSearcHeadView];
    [smsCloudSearchSearcHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudSearchSearchView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:16]);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:56]));
    }];
    [self.smsCloudTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(smsCloudSearchSearcHeadView.mas_bottom);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:497]));
    }];
    [self.smsCloudSearchResultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudTableView.mas_bottom).offset([SmsCloudHead SmsCloudScreewRatio:16]);
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
    self.smsCloudTableView.smsClouds = [SmsCloudDataHelper defaultMag].smsCloudProducts;
    
   
}
- (void)smsCloudResult{
    NSMutableArray * smsCloudResults = [[NSMutableArray alloc] init];
    if (self.search.length) {
        NSArray * products = [SmsCloudDataHelper defaultMag].smsCloudProducts;
        for (int i = 0; i < products.count; i++) {
            SmsCloudProductModel *smsCloudProductMdl = products[i];
            if ([smsCloudProductMdl.name containsString:self.search]) {
                [smsCloudResults addObject:smsCloudProductMdl];
            }
        }
    }
    if (smsCloudResults.count > 0) {
        _smsCloudSearchResultView.hidden = false;
        self.smsCloudSearchResultView.smsClouds = smsCloudResults.copy;
    }else{
        [SVProgressHUD showInfoWithStatus:@"未找到结果"];
        _smsCloudSearchResultView.hidden = true;
    }
    [self smsCloudEndEdit];
}
- (void)smsCloudEndEdit{
    [self.view endEditing:true];
}
@end
